package de.hauck.rtmidlet.views;

import java.util.Calendar;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

import de.hauck.rtmidlet.DownloadImageThread;
import de.hauck.rtmidlet.RTMidlet;
import de.hauck.rtmidlet.utils.Utils;

public class CanvasImage extends CanvasSuper implements CommandListener,
		iCanvas {
	private Font f_small; //, f_med;
	private Image _image;
	private String _name = "";
	//private String _url;
	private String[][] _urls;
	private Image[] _images;
	private boolean[] _isDownloading;
	private String[] _downloadTime;
	private int _index=0;
	private Displayable _dble;

	public CanvasImage(Displayable dble, String[][] urls) {
		super();
		_dble = dble;
		_urls = urls;
		_images = new Image[urls[0].length];
		_isDownloading = new boolean[urls[0].length];
		_downloadTime = new String[urls[0].length];
		f_small = Font.getFont(Font.FONT_STATIC_TEXT, Font.STYLE_PLAIN,
				Font.SIZE_SMALL);
	}
	
	public CanvasImage(Displayable dble, String url) {
		super();
		_url = url;
		_images = new Image[1];
		_isDownloading = new boolean[1];
		_downloadTime = new String[1];
		f_small = Font.getFont(Font.FONT_STATIC_TEXT, Font.STYLE_PLAIN,
				Font.SIZE_SMALL);
	}

	protected void paint(Graphics g) {
		super.paint(g);
		_image=_images[_index];
		if (_image != null) {
			
			g.drawImage(_image, getWidth() / 2, (getHeight() / 2)
					- _image.getHeight() / 2, Graphics.TOP | Graphics.HCENTER);
			g.drawString(_downloadTime[_index], getWidth()/2, getHeight(),Graphics.BOTTOM
				| Graphics.HCENTER);
		} 
		
		if (_isDownloading[_index]) {
			paintDownloadStatus(g, f_small);
		}
		g.drawString(_name, 0, getHeight()
				, Graphics.BOTTOM
				| Graphics.LEFT);
		g.drawString(_urls[1][_index], getWidth(), getHeight()
				, Graphics.BOTTOM
				| Graphics.RIGHT);
	}

	public void commandAction(Command arg0, Displayable arg1) {
		// TODO Auto-generated method stub

	}

	protected void keyPressed(int keyCode) {
		int action = getGameAction(keyCode);
		if (keyCode == KEY_NUM1) {
			downloadPic(_name, _index);
			this.repaint();
			System.out.println("reload");
		} else if (keyCode == KEY_NUM5 || action == FIRE) {
			//System.out.println("change");
			if (_index < _urls[0].length - 1)
				_index++;
			else
				_index = 0;
			if (null == _images[_index] && !_isDownloading[_index])
				downloadPic(_name, _index);
			else {
				_image = _images[_index];
				this.repaint();
			}
		} else if (keyCode == KEY_NUM8) {
			try {
				System.out.println("scale to fullsize");
				_images[_index] = Utils.rescaleImage(_images[_index], getWidth(), getHeight());
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			this.repaint();	
		} else if (keyCode == KEY_NUM9) {
			try {
				System.out.println("rotate by 90");
				_images[_index] = Utils.rotateImage(_images[_index], 90);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			this.repaint();
		} else if (keyCode == KEY_STAR) {
			String[][] strings = {{"5","cycle images"},{"8","fullscreen"},{"9","rotate"}};
			CanvasInfo debugCanvas = new CanvasInfo(this,"Info", strings);
			RTMidlet.getInstance().show(debugCanvas);
		} else if (keyCode == -6 || keyCode == -7) {
			if (_dble instanceof FormAdHoc)
				RTMidlet.getInstance().show(_dble);
			else 
				RTMidlet.getInstance().showMenu();
			
		} else {
			System.out.println("donnu what to do");
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * de.hauck.rtmidlet.views.iCanvas#showPic(javax.microedition.lcdui.Image)
	 */
	public void showPic(Image image, int index) {
		if (image.getWidth()>image.getHeight()) {
			try {
				image=Utils.rotateImage(image, 90);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		_images[index]=image;
		_downloadTime[index]=Utils.convertCal2Time(Calendar.getInstance(), true);
		_isDownloading[index]=false;
		repaint();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see de.hauck.rtmidlet.views.iCanvas#downloadPic(java.lang.String,
	 * java.lang.String)
	 */
	public void downloadPic(String name, int index) {
		_name = name;
		_index = index;
		_image = null;
		repaint();
		System.out.println("download " + _urls[0][index]);		
		_isDownloading[index]=true;
		new DownloadImageThread(_urls[0][index], 0,index,this);
	}

	public void setUrls(String[][] urls) {
		_urls = urls;
		_images = new Image[urls[0].length];
		_isDownloading = new boolean[urls[0].length];
		_downloadTime = new String[urls[0].length];
		
	}

}
